/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Desktop_ItemDetails.java
 *
 * Created on Mar 31, 2011, 7:29:11 PM
 */
package DesktopGUIpackage;

import Controller.DesktopController;
import Controller.DesktopControllerHelper;
import DTO.Item;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JLabel;

/**
 *
 * @author edaype
 */
public class Desktop_ItemDetails extends javax.swing.JPanel {

    private DesktopController desktopController;
    private DesktopControllerHelper desktopControllerHelper;
    private Item itemDetails = null;

    //private List<Item> itemList
    /** Creates new form Desktop_ItemDetails */
    public Desktop_ItemDetails(DesktopControllerHelper desktopControllerHelper, Item item) {
        this.desktopControllerHelper = desktopControllerHelper;
        desktopController = desktopControllerHelper.getDesktopController();
        initComponents();
        itemDetails = item;
        customInit();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        ImagePanel = new javax.swing.JPanel();
        ImageLabel = new javax.swing.JLabel();
        ImageLabel1 = new javax.swing.JLabel();
        DescriptionLabel = new javax.swing.JLabel();
        DescriptionPane = new javax.swing.JScrollPane();
        DescriptionField = new javax.swing.JTextArea();
        ButtonLayout = new javax.swing.JPanel();
        BackButton = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        AddButton = new javax.swing.JButton();
        PriceField = new javax.swing.JTextField();
        ModelLabel = new javax.swing.JLabel();
        ModelField = new javax.swing.JTextField();
        PriceLabel = new javax.swing.JLabel();
        MakeLabel = new javax.swing.JLabel();
        MakeField = new javax.swing.JTextField();
        NameLabel = new javax.swing.JLabel();
        NameField = new javax.swing.JTextField();
        DiscountLabel = new javax.swing.JLabel();
        DiscountField = new javax.swing.JTextField();
        QtyCombo = new javax.swing.JComboBox();

        setBackground(new java.awt.Color(255, 255, 255));

        ImagePanel.setBackground(new java.awt.Color(255, 255, 255));
        ImagePanel.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        ImagePanel.setPreferredSize(new java.awt.Dimension(139, 139));

        javax.swing.GroupLayout ImagePanelLayout = new javax.swing.GroupLayout(ImagePanel);
        ImagePanel.setLayout(ImagePanelLayout);
        ImagePanelLayout.setHorizontalGroup(
            ImagePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(ImagePanelLayout.createSequentialGroup()
                .addComponent(ImageLabel)
                .addContainerGap(298, Short.MAX_VALUE))
            .addGroup(ImagePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(ImagePanelLayout.createSequentialGroup()
                    .addGap(49, 49, 49)
                    .addComponent(ImageLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 200, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(49, Short.MAX_VALUE)))
        );
        ImagePanelLayout.setVerticalGroup(
            ImagePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(ImagePanelLayout.createSequentialGroup()
                .addComponent(ImageLabel)
                .addContainerGap(298, Short.MAX_VALUE))
            .addGroup(ImagePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(ImagePanelLayout.createSequentialGroup()
                    .addGap(49, 49, 49)
                    .addComponent(ImageLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 200, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(49, Short.MAX_VALUE)))
        );

        DescriptionLabel.setFont(new java.awt.Font("Tahoma", 0, 24));
        DescriptionLabel.setText("Description");

        DescriptionField.setColumns(20);
        DescriptionField.setEditable(false);
        DescriptionField.setRows(5);
        DescriptionPane.setViewportView(DescriptionField);

        ButtonLayout.setBackground(new java.awt.Color(255, 255, 255));

        BackButton.setFont(new java.awt.Font("Tahoma", 0, 18));
        BackButton.setText("Back to Search");
        BackButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BackButtonActionPerformed(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Copperplate Gothic Bold", 2, 60));
        jLabel1.setText("Item Details");

        javax.swing.GroupLayout ButtonLayoutLayout = new javax.swing.GroupLayout(ButtonLayout);
        ButtonLayout.setLayout(ButtonLayoutLayout);
        ButtonLayoutLayout.setHorizontalGroup(
            ButtonLayoutLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, ButtonLayoutLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 429, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 188, Short.MAX_VALUE)
                .addComponent(BackButton)
                .addContainerGap())
        );
        ButtonLayoutLayout.setVerticalGroup(
            ButtonLayoutLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(ButtonLayoutLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(ButtonLayoutLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel1)
                    .addComponent(BackButton, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jLabel2.setFont(new java.awt.Font("Tahoma", 0, 24));
        jLabel2.setText("Quantity");

        jLabel3.setFont(new java.awt.Font("Tahoma", 0, 24));
        jLabel3.setText("Select");

        AddButton.setFont(new java.awt.Font("Tahoma", 0, 18));
        AddButton.setText("Add to Cart");
        AddButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                AddButtonActionPerformed(evt);
            }
        });

        PriceField.setEditable(false);
        PriceField.setFont(new java.awt.Font("Tahoma", 0, 24));

        ModelLabel.setFont(new java.awt.Font("Tahoma", 0, 24));
        ModelLabel.setText("Model");

        ModelField.setEditable(false);
        ModelField.setFont(new java.awt.Font("Tahoma", 0, 24));

        PriceLabel.setFont(new java.awt.Font("Tahoma", 0, 24));
        PriceLabel.setText("Price");

        MakeLabel.setFont(new java.awt.Font("Tahoma", 0, 24));
        MakeLabel.setText("Make");

        MakeField.setEditable(false);
        MakeField.setFont(new java.awt.Font("Tahoma", 0, 24));

        NameLabel.setFont(new java.awt.Font("Tahoma", 0, 24));
        NameLabel.setText("Name");

        NameField.setEditable(false);
        NameField.setFont(new java.awt.Font("Tahoma", 0, 24));

        DiscountLabel.setFont(new java.awt.Font("Tahoma", 0, 24));
        DiscountLabel.setText("Sale Price!");

        DiscountField.setEditable(false);
        DiscountField.setFont(new java.awt.Font("Tahoma", 0, 24));

        QtyCombo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10" }));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(ImagePanel, javax.swing.GroupLayout.PREFERRED_SIZE, 300, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(18, 18, 18)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addGroup(layout.createSequentialGroup()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                .addComponent(DiscountLabel)
                                .addComponent(PriceLabel)
                                .addComponent(ModelLabel)
                                .addComponent(MakeLabel)
                                .addComponent(NameLabel))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(DiscountField)
                                .addComponent(PriceField)
                                .addComponent(ModelField)
                                .addComponent(MakeField)
                                .addComponent(NameField, javax.swing.GroupLayout.DEFAULT_SIZE, 352, Short.MAX_VALUE)))
                        .addGroup(layout.createSequentialGroup()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                                .addComponent(jLabel3)
                                .addComponent(jLabel2))
                            .addGap(63, 63, 63)
                            .addComponent(QtyCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(51, 51, 51)
                            .addComponent(AddButton, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addContainerGap())
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(DescriptionLabel)
                    .addContainerGap(698, Short.MAX_VALUE))
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addGroup(layout.createSequentialGroup()
                            .addGap(10, 10, 10)
                            .addComponent(DescriptionPane, javax.swing.GroupLayout.DEFAULT_SIZE, 805, Short.MAX_VALUE))
                        .addComponent(ButtonLayout, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGap(20, 20, 20)))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(ImagePanel, javax.swing.GroupLayout.PREFERRED_SIZE, 300, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(3, 3, 3)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(NameField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(NameLabel))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(MakeField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(MakeLabel))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(ModelField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(ModelLabel))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(PriceField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(PriceLabel))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(DiscountField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(DiscountLabel))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel3)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jLabel2))
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(AddButton, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(QtyCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(DescriptionLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(DescriptionPane, javax.swing.GroupLayout.PREFERRED_SIZE, 115, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(ButtonLayout, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(28, 28, 28))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void BackButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BackButtonActionPerformed
        desktopControllerHelper.SearchScreen();
    }//GEN-LAST:event_BackButtonActionPerformed

    private void AddButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_AddButtonActionPerformed
        int qty = QtyCombo.getSelectedIndex()+1;
        desktopController.addItemToCart(itemDetails, qty );

        desktopControllerHelper.cartScreen();
    }//GEN-LAST:event_AddButtonActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton AddButton;
    private javax.swing.JButton BackButton;
    private javax.swing.JPanel ButtonLayout;
    private javax.swing.JTextArea DescriptionField;
    private javax.swing.JLabel DescriptionLabel;
    private javax.swing.JScrollPane DescriptionPane;
    private javax.swing.JTextField DiscountField;
    private javax.swing.JLabel DiscountLabel;
    private javax.swing.JLabel ImageLabel;
    private javax.swing.JLabel ImageLabel1;
    private javax.swing.JPanel ImagePanel;
    private javax.swing.JTextField MakeField;
    private javax.swing.JLabel MakeLabel;
    private javax.swing.JTextField ModelField;
    private javax.swing.JLabel ModelLabel;
    private javax.swing.JTextField NameField;
    private javax.swing.JLabel NameLabel;
    private javax.swing.JTextField PriceField;
    private javax.swing.JLabel PriceLabel;
    private javax.swing.JComboBox QtyCombo;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    // End of variables declaration//GEN-END:variables

    private void customInit() {

        MakeField.setText(itemDetails.getMake());
        ModelField.setText(itemDetails.getModel());
        NameField.setText(itemDetails.getName());
        PriceField.setText(String.valueOf(itemDetails.getRetailPrice()));
        if (itemDetails.getDiscountAmount() > 0.00) {
            DiscountField.setText(String.valueOf(itemDetails.getRetailPrice()-itemDetails.getDiscountAmount()));
            
        } else {
            DiscountLabel.setVisible(false);
            DiscountField.setVisible(false);
        }

        DescriptionField.setText(itemDetails.getDescription());
        //not working
        DescriptionField.setLineWrap(true);
        DescriptionField.setWrapStyleWord(true);

        BufferedImage image = null;
        try {
            String location = System.getProperty("user.dir");
            System.out.println("url" + itemDetails.getImgURL());
            location += "/src/DesktopGUIpackage/images/" + itemDetails.getImgURL();
            System.out.println(" image loc " + location);
            File imageFile = new File(location);
            image = ImageIO.read(imageFile);
        } catch (IOException e) {
            // todo: log the exception
        }
        BufferedImage resizedImage = resize(image, 280, 280);
        ImageIcon icon = new ImageIcon(resizedImage);
        ImageLabel = new JLabel(icon);
        ImageLabel.setBounds(1, 1, 280, 280);
        ImagePanel.removeAll();
        ImagePanel.add(ImageLabel);
        ImagePanel.validate();
        ImagePanel.repaint();
    }

    private static BufferedImage resize(BufferedImage image, int width, int height) {
        BufferedImage resizedImage = new BufferedImage(width, height,
                BufferedImage.TYPE_INT_ARGB);
        Graphics2D g = resizedImage.createGraphics();
        g.drawImage(image, 0, 0, width, height, null);
        g.dispose();
        return resizedImage;
    }
}
